<?php
/**
 * Created by PhpStorm.
 * User: allen_ios
 * Date: 17/7/14
 * Time: 下午9:25
 */

namespace Task\App\Base;

use Task\Core\Base;
use Task\Core\RedisService;

class BuildBaseInfo extends Base
{
    private $accountId;

    public function __construct($accountId)
    {
        parent::__construct();

        $this->mysql = $this->selectDB('mysql');
        $this->accountId = $accountId;
    }

    /**
     * 通过园区id获取园区信息
     * @param int $commId 园区id
     */
    public function getCommunityInfo($commId)
    {
        if (!$commId) {
            return null;
        }
        //先从缓存中去获取
        $cacheKey = "community_$commId";
        $cacheData = RedisService::hget($cacheKey);
        if ($cacheData) {
            return $cacheData;
        }
        //如果没有就从数据库中去取
        $sql = "select * from gc_community where account_id={$this->accountId} and community_id={$commId} and cut_off=0 limit 1";
        $data = $this->mysql->query($sql)->fetch(\PDO::FETCH_ASSOC);
        if ($data) {
            RedisService::hset($cacheKey,$data);
            return $data;
        } else {
            return null;
        }
    }

    /**
     * 获得园区列表
     * @return mixed
     */
    public function getCommunityList()
    {
        //如果没有就从数据库中去取
        $sql = "select * from gc_community where account_id={$this->accountId} and cut_off=0";
        $data = $this->mysql->query($sql)->fetchAll(\PDO::FETCH_ASSOC);

        return $data;
    }

    /**
     * 获取单个楼栋的信息
     * @param int $buildId 楼栋id
     * @return array
     */
    public function getBuildInfo($buildId)
    {
        if (!$buildId) {
            return null;
        }

        //先从缓存中去获取
        $cacheKey = "building_{$buildId}";
        $cacheData = RedisService::hget($cacheKey);
        if ($cacheData) {
            return $cacheData;
        }

        //如果没有就从数据库中去取
        $sql = "select * from gc_building where account_id={$this->accountId} and building_id={$buildId} and cut_off=0 limit 1";
        $data = $this->mysql->query($sql)->fetch(\PDO::FETCH_ASSOC);
        if ($data) {
            RedisService::hset($cacheKey,$data);
            return $data;
        } else {
            return null;
        }
    }

    /**
     * 获取单个楼层的信息
     * @param int $floorId 楼层id
     * @return mixed
     */
    public function getFloorInfo($floorId)
    {
        if (!$floorId) {
            return null;
        }
        //先从缓存中去获取
        $cacheKey = "floor_$floorId";
        $cacheData = RedisService::hget($cacheKey);
        if ($cacheData) {
            return $cacheData;
        }
        //如果没有就从数据库中去取
        $sql = "select * from gc_floor where account_id={$this->accountId} and floor_id={$floorId} and cut_off=0 limit 1";
        $data = $this->mysql->query($sql)->fetch(\PDO::FETCH_ASSOC);
        if ($data) {
            RedisService::hset($cacheKey,$data);
            return $data;
        } else {
            return null;
        }
    }

    /**
     * 获取单个寝室的信息
     * @param int $roomId 寝室id
     * @return mixed
     */
    public function getRoomInfo($roomId)
    {
        if (!$roomId) {
            return null;
        }
        //先从缓存中去获取
        $cacheKey = "room_$roomId";
        $cacheData = RedisService::hget($cacheKey);
        if ($cacheData) {
            return $cacheData;
        }
        //如果没有就从数据库中去取
        $sql = "select * from gc_room where account_id={$this->accountId} and room_id={$roomId} and cut_off=0 limit 1";
        $data = $this->mysql->query($sql)->fetch(\PDO::FETCH_ASSOC);
        if ($data) {
            RedisService::hset($cacheKey,$data);
            return $data;
        } else {
            return null;
        }
    }

    /**
     * 获得楼幢列表通过园区信息
     * @param $commId
     * @return array|null
     */
    public function getBuildListByCommId($commId)
    {
        if (!$commId) {
            return null;
        }

        $sql = "select * from gc_building where account_id={$this->accountId} and community_id={$commId} and cut_off=0";
        $data = $this->mysql->query($sql)->fetchAll(\PDO::FETCH_ASSOC);
        if (!$data) {
            return null;
        }

        $newBuildList = array();
        foreach ($data as $k => $v) {
            $newBuildList[] = $this->getBuildInfo($v['building_id']);
        }
        return $newBuildList;
    }

    /*
     * 通过楼幢信息$building_id获取当前楼幢的楼层
     * $param $building_id int
     * return array
     */
    public function getFloorListByBuildingId($building_id){
        if (!$building_id) {
            return null;
        }
        $sql = "select * from gc_floor where account_id={$this->accountId} and building_id={$building_id} and cut_off=0";
        $data = $this->mysql->query($sql)->fetchAll(\PDO::FETCH_ASSOC);
        if (!$data) {
            return null;
        }
        $newFloorList = array();
        foreach ($data as $k=>$v) {
            $newFloorList[] = $this->getFloorInfo($v['floor_id']);
        }
        return $newFloorList;
    }

    /*
     * 通过楼幢信息$floor_id获取当前楼层的房间信息
     * $param $floor_id int
     * return array
     */
    public function getRoomListByFloorId($floor_id){
        if (!$floor_id) {
            return null;
        }
        //如果没有就从数据库中去取
        $sql = "select * from gc_room where account_id={$this->accountId} and floor_id={$floor_id} and cut_off=0";
        $data = $this->mysql->query($sql)->fetchAll(\PDO::FETCH_ASSOC);
        if (!$data) {
            return false;
        }
        $newRoomList = array();
        foreach ($data as $k=>$V) {
            $newRoomList[] = $this->getRoomInfo($V['room_id']);
        }

        return $newRoomList;
    }
}